import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadPoolDemo3 {
    public static void main(String[] args) {
        ScheduledExecutorService service= Executors.newScheduledThreadPool(5);
        System.out.println("添加任务时间:"+ LocalDateTime.now());
        scheduleTest(service);
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("执行任务："+LocalDateTime.now());
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },2,4, TimeUnit.SECONDS);
        service.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println("执行时间:"+LocalDateTime.now());
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },2,4,TimeUnit.SECONDS);
    }
    public static void scheduleTest(ScheduledExecutorService service){
        service.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("执行了任务："+LocalDateTime.now());
            }
        },3,TimeUnit.SECONDS);
    }
}
